/**
 * 返回年月日
 * @export
 * @param {Date} date
 * @param {string} [splitor='-']
 * @returns
 */
export function getDate(date, splitor = '-') {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return `${year}${splitor}${addZeroPrefix(month)}${splitor}${addZeroPrefix(day)}`
}

/**
 * 返回时分秒/时分
 * @export
 * @param {*} date
 * @param {boolean} [withSecond=false]
 * @returns
 */
 export function getTime(date, withSecond = false) {
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return withSecond ? `${addZeroPrefix(hour)}:${addZeroPrefix(minute)}:${addZeroPrefix(second)}` : `${hour}:${addZeroPrefix(minute)}`
}

/**
 * 返回时月/日分秒/时分
 * @export
 * @param {*} date
 * @param {boolean} [withSecond=false]
 * @returns
 */
 export function getMonthDay(date, withSecond = false) {
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return withSecond ? `${addZeroPrefix(month)}月${addZeroPrefix(day)}日 ${addZeroPrefix(hour)}:${addZeroPrefix(minute)}:${addZeroPrefix(second)}` : `${addZeroPrefix(month)}月${addZeroPrefix(day)}日 ${hour}:${addZeroPrefix(minute)}`
}

export function getFullDate(date) {
  return `${getDate(date)} ${getTime(date)}`
}

export function isToday(date) {
  return date.toDateString() === new Date().toDateString()
}

// 判断是否为今年
export function isThisYear(time) {
  //今年非昨天
  let timestamp = new Date(time).getTime()
  var tYear=new Date().getFullYear()
  var tYearTime = new Date(tYear, 0, 1).getTime(); //今年第一天
  console.log(tYearTime)
  const date = new Date();
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const today = `${year}/${month}/${day}`; 
  const todayTime = new Date(today).getTime(); // 当天凌晨的时间

  const yesterdayTime = new Date(todayTime - 24 * 60 * 60 * 1000).getTime(); // 昨天凌晨的时间
  console.log(yesterdayTime)
  return timestamp >= tYearTime && timestamp < yesterdayTime;
}

export function changetime(time, type) {
  var d = new Date(time)
  var y = d.getFullYear();
  var m = d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
  var D = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();
  var h = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
  var M = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
  var s = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();
  switch (type) {
    case 1:
      return y + "-" + m + "-" + D + " " + h + ":" + M + ":" + s;
    case 2:
      return y + "-" + m + "-" + D;
    case 3:
      return y + "年" + m + "月" + D + "日  " + h + ":" + M;
    case 4:
      return h + ":" + M;
    case 5:
      return parseInt(y.toString() + m.toString() + D.toString());
    case 6:
      return parseInt(time/(1000*60*60*24));
    default:
      return y + "-" + m + "-" + D + " " + h + ":" + M
  }
}
/**
 * 个位数，加0前缀
 * @param {*} number
 * @returns
 */
function addZeroPrefix(number) {
  return number < 10 ? `0${number}`:number
}
